package com.dream.keigezhushou.Activity.kege.acty;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Fragment.KeRankingFragment;
import com.dream.keigezhushou.Activity.Fragment.MyKeFragment;
import com.dream.keigezhushou.Activity.Fragment.MySingingFragment;
import com.dream.keigezhushou.Activity.Fragment.SingRankingFragment;
import com.dream.keigezhushou.Activity.Uiutils.StringUtils;
import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.kege.adapter.BaseKeAdapter;
import com.dream.keigezhushou.Activity.kege.view.BoxBlurFilter;
import com.dream.keigezhushou.Activity.kege.view.CustomViewPager;
import com.dream.keigezhushou.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.internal.Utils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * k歌，合唱排行榜
 */

public class RankingListActivity extends AppCompatActivity {
    @BindView(R.id.list_lv)
    PullToRefreshListView pullToRefreshListView;
    private ListView mListlv;
    private LinearLayout linearLayout;


    private ImageView returnImag;
    CustomViewPager mViewPager;
    TextView keSongTv;
    ImageView keSongImg;
    TextView singingTv;
    ImageView singingImg;
    LinearLayout yuanChangLinear;
    LinearLayout kgLinear;
    LinearLayout heChangLinear;

    private ArrayList<Fragment> fragmentList;
    private int currIndex;//当前页卡编号
    private int offset;//图片移动的偏移量
    private int bmpW;//横线图片宽度

    private View mHeadView;
    private View mfootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking_list);
        ButterKnife.bind(this);
        mHeadView = UiUtils.inflateView(R.layout.viewpage);
        mfootView = UiUtils.inflateView(R.layout.foot_viewpage);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.DISABLED);

        linearLayout = (LinearLayout) mHeadView.findViewById(R.id.ranking_head_img);
        returnImag = (ImageView) mHeadView.findViewById(R.id.ib_return);
        keSongTv = (TextView) mHeadView.findViewById(R.id.my_ke_song_tv);
        keSongImg = (ImageView) mHeadView.findViewById(R.id.my_ke_song_img);
        singingTv = (TextView) mHeadView.findViewById(R.id.my_singing_tv);
        singingImg = (ImageView) mHeadView.findViewById(R.id.my_singing_img);

        yuanChangLinear = (LinearLayout) mHeadView.findViewById(R.id.yuanchang_linear);
        kgLinear = (LinearLayout) mHeadView.findViewById(R.id.kege_linear);
        heChangLinear = (LinearLayout) mHeadView.findViewById(R.id.hechang_linear);


        mViewPager = (CustomViewPager) mfootView.findViewById(R.id.viewPager);
        mListlv = pullToRefreshListView.getRefreshableView();
        mListlv.addHeaderView(mHeadView);
        mListlv.addFooterView(mfootView);

        initView();
        BaseKeAdapter baseKeAdapter = new BaseKeAdapter(this);
        mListlv.setAdapter(baseKeAdapter);

        Resources r = this.getResources();
        Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.kge_detail);
        linearLayout.setBackground(BoxBlurFilter.Filter(bmp));
        InitViewPager();
        updateTopIcon(0);
        mViewPager.resetHeight(0);
        mViewPager.addHeight(0, 60 * 10);
        mViewPager.addHeight(1, 60 * 10);
    }


    //初始化控件
    public void initView() {
        keSongTv.setText("K歌排行榜");
        singingTv.setText("合唱排行榜");
        returnImag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //我的ke歌点击事件
        keSongTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTopIcon(0);
            }
        });

        //我的合唱
        singingTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTopIcon(1);
            }
        });


        //原唱点击事件
        yuanChangLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


        //ke歌点击事件
        kgLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(StringUtils.KEGESTATE, "加入合唱");
                intent.setClass(RankingListActivity.this, SingingDetailActivity.class);
                startActivity(intent);
            }
        });


        //合唱点击事件
        heChangLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });

    }


    //初始化Fragment控件
    public void InitViewPager() {

        fragmentList = new ArrayList<Fragment>();
        Fragment keFragment = new KeRankingFragment();
        Fragment singingFragment = new SingRankingFragment();
        fragmentList.add(keFragment);
        fragmentList.add(singingFragment);

        //给ViewPager设置适配器
        mViewPager.setAdapter(new KeFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        mViewPager.setCurrentItem(0);//设置当前显示标签页为第一页
        updateTopIcon(0);
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器

    }


    //viewpage选择状态
    public void updateTopIcon(int arg0) {
        if (arg0 == 0) {
            keSongTv.setTextColor(getResources().getColor(R.color.colorRefund));
            keSongImg.setVisibility(View.VISIBLE);
            singingTv.setTextColor(getResources().getColor(R.color.color_666666));
            singingImg.setVisibility(View.INVISIBLE);
            mViewPager.setCurrentItem(0);
        } else if (arg0 == 1) {
            keSongTv.setTextColor(getResources().getColor(R.color.color_666666));
            keSongImg.setVisibility(View.INVISIBLE);
            singingTv.setTextColor(getResources().getColor(R.color.colorRefund));
            singingImg.setVisibility(View.VISIBLE);
            mViewPager.setCurrentItem(1);
        }
    }


    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        private int one = offset * 2 + bmpW;//两个相邻页面的偏移量

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
//             TODO Auto-generated method stub
            updateTopIcon(arg0);
            mViewPager.setCurrentItem(arg0);
            int i = currIndex + 1;
//             Toast.makeText(MainActivity.this, "您选择了第"+i+"个页卡", Toast.LENGTH_SHORT).show();

        }
    }


}


//viewpage的适配器
class KeFragmentPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragmentList;

    public KeFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.fragmentList = list;

    }


    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public Fragment getItem(int arg0) {
        return fragmentList.get(arg0);
    }


}
